import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
// import './assets/theme/theme-green/exam.css'
import 'element-ui/lib/theme-chalk/index.css'
import 'font-awesome/css/font-awesome.min.css'

import VueRouter from 'vue-router'
import store from './store/store'
import Vuex from 'vuex'


import NProgress from 'nprogress'
import 'nprogress/nprogress.css'


import routes from './router/router'
import axios from './common/http'
import _CONSTANT from './common/constant'
import _utils from './common/utils'
import './components/common/js/bootstrap'
import './components/common/css/bootstrap.css'
import * as types from './store/types'
import $ from 'jquery'

Vue.prototype.CONSTANT = _CONSTANT
Vue.prototype.utils = _utils
Vue.prototype.$http = axios

Vue.use(ElementUI);
Vue.use(VueRouter);
Vue.use(Vuex);
Vue.use(CKEDITOR);

NProgress.configure({ showSpinner: false }); //禁用进度环

const router = new VueRouter({
    routes
});

// 页面刷新时，重新赋值token;防止页面刷新时，总是跳转到登录界面
if (window.localStorage.getItem('token')) {

    store.commit(types.LOGIN, window.localStorage.getItem('token'))
}

router.beforeEach((to, from, next) => {

    // NProgress.start(); // 显示进度条

    if (to.meta.requireAuth) { // 判断该路由是否需要登录权限

        if(store.state.token){

            let usertype = JSON.parse(localStorage.getItem("CurrentUser")).usertype;

            if(!to.meta.hidden && to.meta.permission && to.meta.permission.indexOf(usertype)>=0) {

                next();

            }else {
                alert("您无此权限");
            }

        }else {
            next({
                path:'/login',
                query:{redirect:to.fullPath}
            });
        }
    }
    else {
        next();
    }

});

router.afterEach((to, from, next) => {
    // NProgress.done(); // 完成进度条
    document.title = to.name; // 不同路由不同页面标题
});

new Vue({
    el: '#app',
    render: h => h(App),
    router,
    store,
});
